<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<link rel="stylesheet" href="css/bootstrap-3.3.7/css/bootstrap.css" />
<link rel="stylesheet" href="css/priceList.css" />
<jsp:include page="../common/nav.jsp"></jsp:include>
<div class="priceListInfo">
	<div class="infoTitle">
		<span>询价单标题：</span>
		<span>${requestScope.inquiryList.title}</span>
		<span>询价</span>

	</div>
	<div class="infoDetail">
		<input id="id" type="hidden" value="${requestScope.inquiryList.id}">
		<span>询价单号：${requestScope.inquiryList.code}</span>
		<span>询价日期：<fmt:formatDate value="${requestScope.inquiryList.createTime}" pattern="yyyy-MM-dd"/></span>
		<span>共选中比价：${requestScope.inquiryList.checkNum}家</span>
		<span>收到报价数：${requestScope.inquiryList.respondNum}家</span>
		<span>询价单状态：<span class="dataEnd">报价已截止</span></span>
		<span>授标备注：选择报价最低的供应商</span>
	</div>
	<div class="compariseResult">
		<c:if test="${empty requestScope.inquiryList.quoteList}">
			<span>最低含税报价：0</span>
			<span>平均含税报价：0</span>
			<span>最高含税报价：0</span>
		</c:if>
		<c:if test="${not empty requestScope.inquiryList.quoteList}">
			<span>最低含税报价：${"".concat(requestScope.inquiryList.quoteList.get(0).bidAmount).concat("   ").concat(requestScope.inquiryList.quoteList.get(0).company)}</span>
			<span>平均含税报价：${requestScope.inquiryList.averagePrice}</span>
			<span>最高含税报价：${"".concat(requestScope.inquiryList.quoteList.get(requestScope.inquiryList.quoteList.size() - 1).bidAmount).concat("   ").concat(requestScope.inquiryList.quoteList.get(requestScope.inquiryList.quoteList.size() - 1).company)}</span>
		</c:if>
	</div>
</div>
<div class="contentTable">
	<table border="1" cellspacing="0" cellpadding="0">
		<tr>
			<th colspan="4">基本信息</th>
			<th rowspan="2">报价信息</th>
			<th colspan="${fn:length(requestScope.inquiryList.quoteList)}">报价供应商</th>
		</tr>
		<tr>
			<th>行</th>
			<th colspan="2">物料信息</th>
			<th>数量</th>
			<c:forEach items="${requestScope.inquiryList.quoteList}" var="data" varStatus="index">
				<th>
					<span>${data.company}</span>
				</th>
			</c:forEach>
		</tr>
		<c:forEach items="${requestScope.inquiryList.materialList}" var="data" varStatus="index">
			<tr>
				<td rowspan="4">${index.count}</td>
				<td rowspan="4">
					<span class="title">物料名称</span>
					<span>${data.materialName}</span>
					<span class="title">物料描述</span>
					<span>此处为描述</span>
				</td>
				<td rowspan="4">
					<span class="title">物料编号</span>
					<span>${data.materialCode}</span>
					<span class="title">品牌</span>
					<span>${data.materialBrand}</span>
					<span class="title">型号</span>
					<span>${data.materialModel}</span>
				</td>
				<td rowspan="4">
					<span class="title">需求量</span>
					<span>${"".concat(data.materialNumber).concat(requestScope.unitMap[data.materialUnit])}</span>
					<span class="title">授标量</span>
					<span>${"".concat(data.materialNumber).concat(requestScope.unitMap[data.materialUnit])}</span>
					<span class="title">下单量</span>
					<span id="${"orderNum".concat(data.id)}">${"0".concat(requestScope.unitMap[data.materialUnit])}</span>
				</td>
				<td class="title">未税价</td>
				<c:forEach items="${data.quoteList}" var="subData" varStatus="index">
					<c:if test="${subData == data.min && subData != data.max}">
						<td class="companyInfo" style="color: green">${subData}</td>
					</c:if>
					<c:if test="${subData == data.max && subData != data.min}">
						<td class="companyInfo" style="color: red">${subData}</td>
					</c:if>
					<c:if test="${(subData != data.min && subData != data.max) || (subData == data.min && subData == data.max)}">
						<td class="companyInfo">${subData}</td>
					</c:if>
				</c:forEach>
			</tr>
			<tr>
				<c:if test="${!fn:contains(requestScope.inquiryList.quoteRequest, '0')}" >
					<td class="title">税率</td>
					<c:forEach items="${data.taxRateList}" var="subData" varStatus="index">
						<td class="companyInfo">${subData}</td>
					</c:forEach>
				</c:if>
			</tr>
			<tr>
				<td class="title">已下单/授标量</td>
				<c:forEach items="${requestScope.inquiryList.quoteList}" var="subData" varStatus="index">
					<td id="${"sOrderNum".concat(index.count - 1).concat("-").concat(data.id).concat("+").concat(requestScope.inquiryList.quoteList.get(index.count - 1).supplierId).concat("-").concat(data.quoteList.get(index.count - 1)).concat("*").concat(data.materialNumber)}" class="companyInfo sOrderNum">0/${data.materialNumber}</td>
				</c:forEach>
			</tr>
			<tr>
				<td class="title">本次下单量</td>
				<c:forEach items="${requestScope.inquiryList.quoteList}" var="subData" varStatus="index">
					<td class="companyInfo">
						<input
								id="${"".concat(index.count - 1).concat("-").concat(data.id).concat("+").concat(requestScope.inquiryList.quoteList.get(index.count - 1).supplierId).concat("-").concat(data.quoteList.get(index.count - 1)).concat("*").concat(data.materialNumber)}"
								type="radio"
								name="${data.id}"
								onclick="refresh()"
						/>
						<span class="num">${data.materialNumber}</span>
					</td>
				</c:forEach>
			</tr>
		</c:forEach>
		<tr>
			<td rowspan="3" colspan="4">报价小计</td>
			<td>报价小计</td>
			<c:forEach items="${requestScope.inquiryList.quoteList}" var="data" varStatus="index">
				<td>
					<span>${data.bidAmount}</span>
				</td>
			</c:forEach>
		</tr>
		<tr>
			<td>报价排名</td>
			<c:forEach items="${requestScope.inquiryList.quoteList}" var="data" varStatus="index">
				<td>
					<span>${index.count}</span>
				</td>
			</c:forEach>
		</tr>
		<tr>
			<td>最低标价的数</td>
			<c:forEach items="${requestScope.inquiryList.quoteList}" var="data" varStatus="index">
				<td>
					<span>${data.minNum}</span>
				</td>
			</c:forEach>
		</tr>
		<tr>
			<td rowspan="2" colspan="4">授标小计</td>
			<td>授标标的数</td>
			<c:forEach items="${requestScope.inquiryList.quoteList}" var="data" varStatus="index">
				<td>
					<span id="${"count".concat(index.count - 1)}">0</span>
				</td>
			</c:forEach>
		</tr>
		<tr>
			<td>授标总金额</td>
			<c:forEach items="${requestScope.inquiryList.quoteList}" var="data" varStatus="index">
				<td>
					<span id="${"money".concat(index.count - 1)}">0.00</span>
				</td>
			</c:forEach>
		</tr>
		<tr>
			<td rowspan="4" colspan="4">供应商信息</td>
			<td>所在地区</td>
			<c:forEach items="${requestScope.inquiryList.quoteList}" var="data" varStatus="index">
				<td>
					<span>${data.address}</span>
				</td>
			</c:forEach>
		</tr>
		<tr>
			<td>联系人</td>
			<c:forEach items="${requestScope.inquiryList.quoteList}" var="data" varStatus="index">
				<td>
					<span>${data.name}</span>
				</td>
			</c:forEach>
		</tr>
		<tr>
			<td>联系方式</td>
			<c:forEach items="${requestScope.inquiryList.quoteList}" var="data" varStatus="index">
				<td>
					<span>${data.tel}</span>
				</td>
			</c:forEach>
		</tr>
		<tr>
			<td>注册资金</td>
			<c:forEach items="${requestScope.inquiryList.quoteList}" var="data" varStatus="index">
				<td>
					<span>${data.registeCapital}</span>
				</td>
			</c:forEach>
		</tr>
	</table>
</div>
<div class="totalInfo">
	已选中授标供应商<span id="supplierNum">0</span>家，物料<span id="materialNum">0</span>款，合计金额<span id="totalMoney">0.00</span>
	<button onclick="submit()">直接订购</button>
</div>
<script src="js/CalcEval.js" type="text/javascript" charset="utf-8"></script>
<script>
	var ce = new CalcEval();
	var materials = [];
	var materialNum = ${requestScope.inquiryList.materialList.size()};
	var supplierNum = ${requestScope.inquiryList.quoteList.size()};

	/**
	 * 刷新页面信息
	 */
	function refresh() {
		for (let i = 0; i < materials.length; i ++) {
			$("#orderNum" + materials[i]).text("0");
		}
		let sOrderNum = $(".sOrderNum");
		for (let i = 0; i < sOrderNum.length; i ++) {
			let html = sOrderNum[i].innerHTML;
			sOrderNum[i].innerHTML = 0 + "/" + html.split("/")[1];
		}
		let suppliers = [];
		materials = [];
		let totalMoney = 0;
		let radio = $("input[type='radio']:checked");
		for (let i = 0; i < supplierNum; i ++) {
			let num = 0;
			let money = 0;
			for (let j = 0; j < radio.length; j ++) {
				let id = radio[j].id;
				let supplierId = id.split("-")[1].split("+")[1];
				if (suppliers.indexOf(supplierId) === -1) {
					suppliers.push(supplierId);
				}
				let materialId = id.split("-")[1].split("+")[0];
				if (materials.indexOf(materialId) === -1) {
					materials.push(materialId);
				}
				if (id.startsWith(i + "-")) {
					for (let i = 0; i < sOrderNum.length; i ++) {
						if (sOrderNum[i].id == "sOrderNum" + id) {
							let html = sOrderNum[i].innerHTML.split("/")[1];
							sOrderNum[i].innerHTML = html + "/" + html;
						}
					}
					$("#orderNum" + id.split("-")[1].split("+")[0]).text(id.split("-")[2].split("*")[1]);
					money = ce.eval(money + " + (" + id.split("-")[2] + ")");
					totalMoney = ce.eval(totalMoney + " + (" + id.split("-")[2] + ")");
					num ++;
				}
			}
			$("#count" + i).text(num);
			$("#money" + i).text(money % 1 === 0 ? (money + ".00") : money);
		}
		$("#supplierNum").text(suppliers.length);
		$("#materialNum").text(materials.length);
		$("#totalMoney").text(totalMoney % 1 === 0 ? (totalMoney + ".00") : totalMoney);
	}

	/**
	 * 提交
	 */
	function submit() {
		let radio = $("input[type='radio']:checked");
		if (radio.length != materialNum) {
			redragonJS.alert("部分物资还未选择供应商!");
			return;
		}
		let data = {};
		data.id = $("#id").val();
		let subs = [];
		for (let i = 0; i < radio.length; i ++) {
			let sub = {};
			sub.id = radio[i].id.split("-")[1].split("+")[0];
			sub.supplierId = radio[i].id.split("-")[1].split("+")[1];
			sub.price = radio[i].id.split("-")[2].split("*")[0];
			subs.push(sub);
		}
		data.sub = subs;
		$.ajax({
			type:'post',
			url : "web/inquiryList/priceParity",
			contentType : "application/json",
			data : JSON.stringify(data),
			dataType : 'json',
			async : true,
			success : function(data) {
				if(data.code == 1) {
					redragonJS.alert("操作成功!");
					setTimeout(function(){
						window.location.href='web/inquiryList/getPageList'
					},1000);
				} else {
					redragonJS.alert(data.msg);
				}
			}
		});
	}
</script>